
from 银行取款系统简易版.user import User
from 银行取款系统简易版.card import Card
import random


class ATM(object):

    def __init__(self):
        self.allUser = {}

    #开户
    def createUser(self):
        name = input("请输入姓名:")
        idCard = input("请输入身份证:")
        phone = input("请输入电话:")

        money = float(input("请输入预存款金额:"))
        if money <= 0:
            print("预存款金额有误,存储失败!")
            return -1

        password = input("请输入密码:")
        if not self.checkPassword(password):
            print("密码错误,开户失败!")
            return -1

        # 获取卡id
        cardId = self.randomCardId()

        # 卡信息
        card = Card(cardId, password, money)

        # 用户信息
        user = User(name, idCard, phone, card)

        # 把用户存起来
        self.allUser[cardId] = user

        print("开户成功,卡号(%s)" % cardId)

        return 0

    #查询
    def searchAccount(self):
        count = 0
        user = None
        while count < 3:
            cardId = input("请输入您的卡号:")
            user = self.allUser.get(cardId)
            if not user:
                print("卡号不存在,查询错误!")
                count += 1
            else:
                if user.card.lock:
                    print("该卡号处于锁定状态,请先解锁!")
                    return -1
                break
        else:
            print("失败次数过多!")
            return -1

        card = user.card
        password = input("请输入密码:")
        if password != card.password:
            if not self.checkPassword(card.password):
                print("查询失败!")
                return -1

        print("查询成功")
        print("卡号信息: 卡号(%s) , 余额(%f)" % (card.cardId, float(card.money)))

    #取款
    def getMoney(self):
        cardIdCount = 0

        user = None
        while cardIdCount < 3:
            cardId = input("请输入您的卡号:")
            user = self.allUser.get(cardId)
            if not user:
                print("该卡号不存在!")
                cardIdCount += 1
            else:
                if user.card.lock:
                    print("该卡号处于锁定状态,请先解锁!")
                    return -1
                break
        else:
            print("错误次数过多!")
            return -1

        passwordCount = 0
        while passwordCount < 3:
            password = input("请输入您的密码:")
            if user.card.password != password:
                print("密码输入错误!")
                passwordCount += 1
            else:
                break
        else:
            print("错误次数过多!")
            return -1

        moneyCount = 0
        while moneyCount < 3:
            money = input("请输入取款金额:")

            moneyCount += 1
            if float(money) <= 0:
                print("请输入正确的金额!")
            elif user.card.money < float(money):
                print("余额不足以取出,余额(%f)" % user.card.money)
            else:
                user.card.money = (float(user.card.money) - float(money))
                print("取款成功,卡余额:%f" % user.card.money)
                break
        else:
            print("错误次数过多!")
            return -1

    #存储
    def setMoney(self):
        cardIdCount = 0

        user = None
        while cardIdCount < 3:
            cardId = input("请输入您的卡号:")
            user = self.allUser.get(cardId)
            if not user:
                print("该卡号不存在!")
                cardIdCount += 1
            else:
                if user.card.lock:
                    print("该卡号处于锁定状态,请先解锁!")
                    return -1
                break
        else:
            print("错误次数过多!")
            return -1

        passwordCount = 0
        while passwordCount < 3:
            password = input("请输入您的密码:")
            if user.card.password != password:
                print("密码输入错误!")
                passwordCount += 1
            else:
                break
        else:
            print("错误次数过多!")
            return -1


        moneyCount = 0
        while moneyCount < 3:
            money = input("请输入存储金额:")

            moneyCount += 1
            if float(money) <= 0:
                print("请输入正确的金额!")
            else:
                user.card.money = (float(user.card.money) + float(money))
                print("存款成功,卡余额:%f" % user.card.money)
                break
        else:
            print("错误次数过多!")
            return -1

    #转账
    def transferMoney(self):
        cardIdCount = 0

        user = None
        while cardIdCount < 3:
            cardId = input("请输入您的卡号:")
            user = self.allUser.get(cardId)
            if not user:
                print("该卡号不存在!")
                cardIdCount += 1
            else:
                if user.card.lock:
                    print("该卡号处于锁定状态,请先解锁!")
                    return -1
                break
        else:
            print("错误次数过多!")
            return -1

        passwordCount = 0
        while passwordCount < 3:
            password = input("请输入您的密码:")
            if user.card.password != password:
                print("密码输入错误!")
                passwordCount += 1
            else:
                break
        else:
            print("错误次数过多!")
            return -1

        cCount = 0
        u = None
        while cCount < 3:
            cId = input("请输入转账账号:")

            cCount += 1
            u = self.allUser.get(cId)
            if not u:
                print("转账失败:转账账号不存在!")
            elif u.card.lock:
                print("转账失败:转账账号处于锁定状态!")
            else:
                break
        else:
            print("错误次数过多!")
            return -1

        moneyCount = 0
        while moneyCount < 3:
            money = input("请输入转账金额:")

            moneyCount += 1
            if float(money) <= 0:
                print("请输入正确的金额!")
            else:
                user.card.money = (float(user.card.money) - float(money))
                u.card.money = (float(u.card.money) + float(money))
                print("转账成功,卡余额:%f" % user.card.money)
                break
        else:
            print("错误次数过多!")
            return -1


    #改密
    def modifyPassword(self):
        cardIdCount = 0
        while cardIdCount < 3:
            cardId = input("请输入您的卡号:")
            user = self.allUser.get(cardId)
            if not user:
                print("该卡号不存在!")
                cardIdCount += 1
            else:
                if user.card.lock:
                    print("该卡号处于锁定状态,请先解锁!")
                    return -1
                break
        else:
            print("错误次数过多!")
            return -1

        passwordCount = 0
        while passwordCount < 3:
            password = input("请输入您的密码:")
            if user.card.password != password:
                print("密码输入错误!")
                passwordCount += 1
            else:
                if user.card.lock:
                    print("该卡号处于锁定状态,请先解锁!")
                    return -1
                break
        else:
            print("错误次数过多!")
            return -1

        passCount = 0;
        while passCount < 3:
            newPassword = input("请输入新密码:")
            twoPassword = input("请再次输入新密码:")
            if newPassword != twoPassword:
                print("两次输入不一致!")
                passCount += 1
                if passCount == 3:
                    print("错误次数过多!")
                    return -1
            else:
                user.card.password = newPassword
                break

        print("修改密码成功!")

    #锁定
    def lock(self):
        cardIdCount = 0
        while cardIdCount < 3:
            cardId = input("请输入您的卡号:")
            user = self.allUser.get(cardId)
            if not user:
                print("该卡号不存在!")
                cardIdCount += 1
            else:
                if user.card.lock:
                    print("该卡号已是锁定状态,无需再次锁定!")
                    return -1
                break
        else:
            print("错误次数过多!")
            return -1

        passwordCount = 0
        while passwordCount < 3:
            password = input("请输入您的密码:")
            if user.card.password != password:
                print("密码输入错误!")
                passwordCount += 1
            else:
                user.card.lock = True
                print("该卡号锁定成功!")
                break
        else:
            print("错误次数过多!")
            return -1

    #解锁
    def unlock(self):
        cardIdCount = 0
        while cardIdCount < 3:
            cardId = input("请输入您的卡号:")
            user = self.allUser.get(cardId)
            if not user:
                print("该卡号不存在!")
                cardIdCount += 1
            else:
                if not user.card.lock:
                    print("该卡号已是正常状态,无需解锁!")
                    return -1
                break
        else:
            print("错误次数过多!")
            return -1

        passwordCount = 0
        while passwordCount < 3:
            password = input("请输入您的密码:")
            if user.card.password != password:
                print("密码输入错误!")
                passwordCount += 1
            else:
                user.card.lock = False
                print("该卡号解锁成功!")
                break
        else:
            print("错误次数过多!")
            return -1

    #补卡
    def reCreateAccount(self):
        cardId = input("请输入原卡号:")
        user = self.allUser.get(cardId)
        if not user:
            print("该卡号不存在,补卡失败!")
            return -1
        elif user.card.lock:
            print("该卡号处于锁定状态,请先解锁!")
            return -1

        idCard = input("请输入您的身份证信息:")
        count = 0;
        while count < 3:
            if user.idCard != idCard:
                idCard = input("身份证信息错误,请重新输入:")
                count += 1
            else:
                newCardId = self.randomCardId()
                user.card.cardId = newCardId
                self.allUser.pop(cardId)
                self.allUser[newCardId] = user

                print("补卡成功,请牢记新卡号: %s" % newCardId)
                break
        else:
            print("输入错误次数过多!")
            return -1

    #销户
    def delAccount(self):
        cardIdCount = 0
        while cardIdCount < 3:
            cardId = input("请输入您的卡号:")
            user = self.allUser.get(cardId)
            if not user:
                print("该卡号不存在!")
                cardIdCount += 1
            else:
                break
        else:
            print("错误次数过多!")
            return -1


        passwordCount = 0
        while passwordCount < 3:
            password = input("请输入您的密码:")
            if user.card.password != password:
                print("密码输入错误!")
                passwordCount += 1
            else:
                break
        else:
            print("错误次数过多!")
            return -1

        confirm = input("确定注销账户?Y/N :")
        while True:
            if confirm == "Y" or confirm == "y":
                self.allUser.pop(cardId)
                print("销户成功!")
                return 0
            elif confirm == "N" or confirm == "n":
                return -1
            else:
                confirm = input("请输入Y/N :")

    #退出
    def quit(self):
        pass


    # ATM操作
    def atmAction(self ,action):
        if action == "1":
            # print("开户")
            self.createUser()
        elif action == "2":
            # print("查询")
            self.searchAccount()
        elif action == "3":
            # print("取款")
            self.getMoney()
        elif action == "4":
            # print("存储")
            self.setMoney()
        elif action == "5":
            # print("转账")
            self.transferMoney()
        elif action == "6":
            # print("改密")
            self.modifyPassword()
        elif action == "7":
            # print("锁定")
            self.lock()
        elif action == "8":
            # print("解锁")
            self.unlock()
        elif action == "9":
            # print("补卡")
            self.reCreateAccount()
        elif action == "0":
            # print("销户")
            self.delAccount()
        elif action == "t":
            # print("退出")
            self.quit()


    # 验证密码
    def checkPassword(self , password):
        for i in range(3):
            checkPass = input("请再次输入密码:")
            if checkPass == password:
                return True
        print("输入密码不匹配!")
        return False

    # 随机卡号
    def randomCardId(self):
        while True:
            str = ""
            for i in range(6):
                ch = chr(random.randrange(ord('0'), ord('9')+1))
                str += ch

            if not self.allUser.get(str):
                return str

